import { contextBridge, ipcRenderer } from 'electron';

// 监听主进程日志
ipcRenderer.on('main-process-log', (event, logMessage) => {
  // 在渲染进程控制台中显示主进程日志
  console.log(`🔧 [主进程] ${logMessage}`);
});

contextBridge.exposeInMainWorld('electronAPI', {
  // 获取应用版本
  getAppVersion: () => process.env.APP_VERSION || '1.0.0',
  platform: process.platform,

  // 配置相关API
  selectDirectory: () => ipcRenderer.invoke('select-directory'),
  selectFile: () => ipcRenderer.invoke('select-file'),
  getConfig: () => ipcRenderer.invoke('get-config'),
  saveConfig: (config: any) => ipcRenderer.invoke('save-config', config),

  // 文件系统相关API
  readdir: (directoryPath: string) => ipcRenderer.invoke('readdir', directoryPath),
  stat: (filePath: string) => ipcRenderer.invoke('stat', filePath),
  readFileAsArrayBuffer: (filePath: string) => ipcRenderer.invoke('readFileAsArrayBuffer', filePath),

  // 主进程文件监听相关API
  startListening: (directoryPath: string) => ipcRenderer.invoke('start-listening', directoryPath),
  stopListening: () => ipcRenderer.invoke('stop-listening'),

  // 监听主进程发来的新文件通知
  onNewFileDetected: (callback: (fileInfo: any) => void) => {
    ipcRenderer.on('new-file-detected', (event, fileInfo) => callback(fileInfo));
  },

  // 移除新文件监听器
  removeNewFileListener: () => {
    ipcRenderer.removeAllListeners('new-file-detected');
  }
});